package com.tennis.tennisreservation;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.context.event.EventListener;
import org.springframework.scheduling.annotation.EnableScheduling;

import jakarta.annotation.PostConstruct;
import java.io.File;

@SpringBootApplication
@EnableScheduling
public class TennisReservationApplication {
	
	private static final Logger logger = LoggerFactory.getLogger(TennisReservationApplication.class);

	public static void main(String[] args) {
		// 创建必要的目录
		createRequiredDirectories();
		
		SpringApplication application = new SpringApplication(TennisReservationApplication.class);
		application.run(args);
		
		logger.info("网球预约系统已成功启动");
	}
	
	@EventListener(ApplicationReadyEvent.class)
	public void onApplicationReady() {
		logger.info("应用程序已就绪，可以接收请求");
	}
	
	private static void createRequiredDirectories() {
		// 创建日志目录
		createDirectoryIfNotExists("./logs");
		// 创建上传目录
		createDirectoryIfNotExists("./uploads");
	}
	
	private static void createDirectoryIfNotExists(String path) {
		File directory = new File(path);
		if (!directory.exists()) {
			boolean created = directory.mkdirs();
			if (created) {
				System.out.println("Created directory: " + path);
			} else {
				System.err.println("Failed to create directory: " + path);
			}
		}
	}
}
